package cn.ronghuanet.auth.interceptor;

import cn.ronghuanet.auth.context.LoginContext;
import cn.ronghuanet.auth.mapper.RolePermissionMapper;
import cn.ronghuanet.base.annotation.RonghuanetPermission;
import cn.ronghuanet.org.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.List;

@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    /*
        返回值是一个boolean
            false: 拦截  请求就不会到达目标Controller 里面的方法
            true : 放行
        1.获取请求头 TOKEN
        2.后台验证 TOKEN

     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //1.获取请求头 TOKEN
        String token = request.getHeader("TOKEN");
        //2.token 为空
        if(token==null){//用户就是没有登录
            //返回一个没有登录的特定的JSON,前端收到了这个json以后 跳转到登录页面
            //{"success":false,"msg":"noLogin"}
            response.setContentType("application/json;charset=utf-8");
            PrintWriter writer = response.getWriter();
            writer.print("{\"success\":false,\"msg\":\"noLogin\"}");
            return false;
        }
        //3.使用token不能在map里面获取对象
        Employee employee = LoginContext.context.get(token);
        if (employee==null){
            response.setContentType("application/json;charset=utf-8");
            PrintWriter writer = response.getWriter();
            //前端携带了token  但是后台的Map找不数据,说明用户的token过期了
            writer.print("{\"success\":false,\"msg\":\"noLogin\"}");
            return false;
        }

        //4.验证当前的登录用户访问的功能是否有权限,有 放行 ,没有  拦截
        //4.1 检查当前访问的功能是否需要做权限检查
        HandlerMethod  handlerMethod= (HandlerMethod) handler;
        //4.2 获取用户调用的真实的Method
        Method method = handlerMethod.getMethod();
        //4.3 获取method上面的权限注解
        RonghuanetPermission annotation = method.getAnnotation(RonghuanetPermission.class);
        if (annotation!=null){//用户访问的目标方法需要做权限检查
            //5.当前的登录用户是否有访问当前资源的权限
            //5.1 当前用户拥有的资源权限
            List<String> permissionSn = rolePermissionMapper.getPermissionSnByEmpId(employee.getId());
            //5.2 获取当前用户访问资源sn
            String methodName = method.getName();
            Class<?> beanType = handlerMethod.getBeanType();
            String className = beanType.getSimpleName();
            if (permissionSn.contains(className+":"+methodName)){
                return true;
            }else {
                //当前的登录用户没有权限来访问目标资源
                response.setContentType("application/json;charset=utf-8");
                PrintWriter writer = response.getWriter();
                //前端携带了token  但是后台的Map找不数据,说明用户的token过期了
                writer.print("{\"success\":false,\"msg\":\"noPermission\"}");
                return false;
            }
        }
        return true;
    }
}
